<template>
  <div id="active-container">
    <el-card>
      <template #header>
        <div class="clearfix">
          <span>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>百叶管理</el-breadcrumb-item>
              <el-breadcrumb-item>活动报名</el-breadcrumb-item>
            </el-breadcrumb></span
          >
        </div>
      </template>
      <div>
        <el-select
          v-model="activityBlock"
          size="small"
          clearable
          placeholder="请选择活动板块"
          style="width: 150px; margin-left: 15px"
          @change="activityBlockChange"
        >
          <el-option
            v-for="item in activityBlocks"
            :key="item.collageNum"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select
          v-model="complete"
          size="small"
          clearable
          placeholder="请选择报名是否完成"
          style="width: 180px; margin-left: 15px"
          @change="completeChange"
        >
          <el-option label="未完成" :value="1"> </el-option>
          <el-option label="已完成" :value="0"> </el-option>
        </el-select>
        <el-input
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="keyword"
          size="small"
          style="width: 200px; margin-left: 15px"
        >
        </el-input>
      </div>
      <el-table
        :data="enrolls"
        v-loading="loading"
        :height="height"
        :stripe="true"
        style="width: 100%; top: 15px"
      >
        <el-table-column label="学号" width="120" fixed="left" prop="studentId">
        </el-table-column>

        <el-table-column
          prop="studentName"
          width="80"
          label="姓名"
          fixed="left"
        >
        </el-table-column>
        <el-table-column
          label="活动名"
          prop="activityName"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="活动板块"
          prop="activityBlockValue"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="活动开始时间"
          prop="startTime"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="endTime"
          show-overflow-tooltip
          label="活动结束时间"
        >
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="最近更新"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column width="80" label="是否完成" fixed="right">
          <template #default="scope">
            <el-tag v-if="scope.row.complete == 0" type="success"
              >已完成</el-tag
            >
            <el-tag type="danger" v-else>未完成</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin-top: 30px">
        <el-pagination
          @size-change="sizeChange"
          @current-change="pageChange"
          :current-page="page"
          :page-sizes="[15, 30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :disabled="loading"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import { reactive, toRefs, watch } from "vue";
import { BaiyeService, Enroll } from "@/api/baiye";
import Code from "@/consts/common";

export default {
  name: "Active",
  setup() {
    const enrolls: Enroll[] = [];
    let data = reactive({
      height: document.documentElement.clientHeight - 280,
      keyword: "",
      loading: false,
      total: 0,
      page: 1,
      size: 15,
      enrolls: enrolls,
      activityBlock: null,
      complete: null,
      activityBlocks: [
        {
          value: 0,
          label: "博学",
        },
        {
          value: 1,
          label: "尽美",
        },
        {
          value: 2,
          label: "笃行",
        },
        {
          value: 3,
          label: "明德",
        },
      ],
      pageChange: (page: number) => {
        data.page = page;
        listEnrolls(
          data.page,
          data.size,
          data.keyword,
          // @ts-ignore
          data.activityBlock,
          data.complete
        );
      },

      sizeChange: (size: number) => {
        data.size = size;
        listEnrolls(
          data.page,
          data.size,
          data.keyword,
          // @ts-ignore
          data.activityBlock,
          data.complete
        );
      },
      activityBlockChange(id: number) {
        // @ts-ignore
        data.activityBlock = id;
        listEnrolls(
          data.page,
          data.size,
          data.keyword,
          // @ts-ignore
          data.activityBlock,
          data.complete
        );
      },
      completeChange(id: number) {
        // @ts-ignore
        data.complete = id;
        listEnrolls(
          data.page,
          data.size,
          data.keyword,
          // @ts-ignore
          data.activityBlock,
          data.complete
        );
      },
    });
    const listEnrolls = (
      page: number = 1,
      size: number = 15,
      keyword: string = "",
      // @ts-ignore
      activityBlock: number = null,
      // @ts-ignore
      complete: number = null
    ) => {
      data.loading = true;
      BaiyeService.listEnrolls({
        page,
        size,
        keyword,
        activityBlock,
        complete,
      }).then((res) => {
        if (res.code == Code.CODE_SUCCESS) {
          data.enrolls = res.data.data;
          data.total = res.data.total;
        }
        data.loading = false;
      });
    };

    listEnrolls();
    watch(
      () => data.keyword,
      (newValue, oldVlaue) => {
        // @ts-ignore
        listEnrolls(
          data.page,
          data.size,
          data.keyword,
          // @ts-ignore
          data.activityBlock,
          data.complete
        );
      }
    );

    const getHeight = () => {
      data.height = document.documentElement.clientHeight - 280;
    };
    window.addEventListener("resize", getHeight, false);
    return {
      ...toRefs(data),
    };
  },
};
</script>

<style lang="less" scoped>
</style>

<style lang="less">
.active-dialog {
  .el-card__body {
    padding: 5px 15px !important;
  }
  .el-dialog__body {
    padding: 20px 20px;
  }
}
</style>
